﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using SMARTMobile.Common;
using SMARTMobile.Models;
using SMARTMobile.Models.Local;
using SMARTMobile.Models.UI;
using SMARTMobile.WinPhone;
using SMARTMobile.Services.SMARTMobileServiceReference;
using System.Collections.ObjectModel;
using SmartMobile.Data;

namespace SMARTMobile.ViewModels
{
    public class CapNhatKhachHangViewModel : ViewModelBase
    {
        #region Fields

        SmartMobileDBContext _smartMobileContext;
        private readonly ProvinceModel EmptyProvince = new ProvinceModel();
        private readonly TownModel EmptyTown = new TownModel();

        private readonly List<TownModel> _townList = new List<TownModel>();

        private ARCUST _arcusModel;
        private string _customerName;
        private string _shopName;
        private string _phone;
        private string _address;
        private string _street;
        private string _ward;

        private ObservableCollection<PROVINCE> _provinceCollection;
        private ObservableCollection<TOWN> _townCollection;
        private ObservableCollection<StrategicTypeModel> _strategicCollection;
        private ObservableCollection<ARVolumeLevel> _volumeCollection;
        private ObservableCollection<AROutletStatusModel> _statusCollection;

        private ObservableCollection<City> _cityCollection;
        private ObservableCollection<Area> _areaCollection;
        private ObservableCollection<Region> _regionCollection;
        private ObservableCollection<GFKArea> _gfkCollection;
        private ObservableCollection<Tier> _tierCollection;
        private ObservableCollection<PhoneVendor> _operatorCollection;

        private PROVINCE _selectedProvince;
        private TOWN _selectedTown;
        private StrategicTypeModel _selectedStrategic;
        private ARVolumeLevel _selectedVolume;
        private AROutletStatusModel _selectedStatus;

        private City _selectedCity;
        private Area _selectedArea;
        private Region _selectedRegion;
        private GFKArea _selectedGFK;
        private Tier _selectedTier;
        private PhoneVendor _selectedOperator;

        private int _noOfSalesStaff;
        private string _email;
        private string _fax;
        private double _shopWidth;
        private double _shopLong;
        private string _retailChain;
        private bool _isHeadquarterofchain;

        private bool _isGPS;
        private bool _isEdit;
        private bool _isApprove;
        #endregion

        public CapNhatKhachHangViewModel()
        {
            base.IsBusy = true;
            this.SaveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteSaveCommand
            };

            this.ThoatCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteThoatCommand
            };

            this.DuyetCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteDuyetCommand
            };

            this.DeleteCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteDeleteCommand
            };

            //this.StrategicCollection = new ObservableCollection<StrategicTypeModel>
            //{
            //    //new StrategicTypeModel {  }
            //};

            //this.StatusCollection = new ObservableCollection<AROutletStatusModel>
            //{
            //    new AROutletStatusModel { StatusType = "001", Description = "Đang có giao dịch" },
            //    new AROutletStatusModel { StatusType = "002", Description = "Có bán hàng, nhưng chưa giao dịch với NVBH" },
            //    new AROutletStatusModel { StatusType = "003", Description = "Rất có tiềm năng (đạt 2/3 yếu tố)" },
            //    new AROutletStatusModel { StatusType = "004", Description = "Có tiềm năng (1/3 yếu tố)" }
            //};

            //this.VolumeCollection = new ObservableCollection<ARVolumeLevel>
            //{
            //    new ARVolumeLevel { Level = "001", Description = "Rất lớn > 100 tr" },
            //    new ARVolumeLevel { Level = "002", Description = "Lớn 10 tr -> 100 tr" },
            //    new ARVolumeLevel { Level = "003", Description = "Vừa 5 tr -> 10 tr" },
            //    new ARVolumeLevel { Level = "004", Description = "Nhỏ < 5 tr" }
            //};
            this.CreateConnection();

            var province = this._smartMobileContext.PROVINCE.FirstOrDefault();
            var town = this._smartMobileContext.TOWN.FirstOrDefault();

            if (province == null)
            {
                this.ShowMessage("Vui lòng cập nhật thông tin Quận Huyện");
                return;
            }

            if (town == null)
            {
                this.ShowMessage("Vui lòng cập nhật thông tin Quận Huyện");
                return;
            }

            // Tao moi khach hang or sua thong tin khach hang
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                this.ShopName = GlobalInformation.SelectedTCARCUS.ShopOwner;
                this.CustomerName = GlobalInformation.SelectedTCARCUS.Customername;
                this.Phone = GlobalInformation.SelectedTCARCUS.Phone;
                this.Address = GlobalInformation.SelectedTCARCUS.Address;
                this.Street = GlobalInformation.SelectedTCARCUS.Street;
                this.Ward = GlobalInformation.SelectedTCARCUS.Ward;

                // Lay du lieu binding combobox
                this.ProvinceCollection = new ObservableCollection<PROVINCE>(this._smartMobileContext.PROVINCE.ToList());

                if (string.IsNullOrEmpty(GlobalInformation.SelectedTCARCUS.Provice))
                {
                    this.SelectedProvince = this.ProvinceCollection[0];
                }
                else
                {
                    this.SelectedProvince = this.ProvinceCollection.Where(p => p.ProvinceKey == GlobalInformation.SelectedTCARCUS.Provice).FirstOrDefault();
                }

                if (string.IsNullOrEmpty(GlobalInformation.SelectedTCARCUS.Town))
                {
                    this.SelectedTown = this.TownCollection[0];
                }
                else
                {
                    this.SelectedTown = this.TownCollection.Where(t => t.Townkey == GlobalInformation.SelectedTCARCUS.Town).FirstOrDefault();
                }

                // Lay du lieu binding combobox
                this.CityCollection = new ObservableCollection<City>(this._smartMobileContext.City.ToList());
                this.AreaCollection = new ObservableCollection<Area>(this._smartMobileContext.Area.ToList());
                this.RegionCollection = new ObservableCollection<Region>(this._smartMobileContext.Region.ToList());
                this.GFKCollection = new ObservableCollection<GFKArea>(this._smartMobileContext.GFKArea.ToList());
                this.TierCollection = new ObservableCollection<Tier>(this._smartMobileContext.Tier.ToList());
                this.OperatorCollection = new ObservableCollection<PhoneVendor>(this._smartMobileContext.PhoneVendor.ToList());
            }
            else
            {
                // Lay du lieu binding combobox
                this.ProvinceCollection = new ObservableCollection<PROVINCE>(this._smartMobileContext.PROVINCE.ToList());
                this.SelectedProvince = this.ProvinceCollection[0];
                this.SelectedTown = this.TownCollection[0];
                this.CityCollection = new ObservableCollection<City>(this._smartMobileContext.City.ToList());
                this.AreaCollection = new ObservableCollection<Area>(this._smartMobileContext.Area.ToList());
                this.RegionCollection = new ObservableCollection<Region>(this._smartMobileContext.Region.ToList());
                this.GFKCollection = new ObservableCollection<GFKArea>(this._smartMobileContext.GFKArea.ToList());
                this.TierCollection = new ObservableCollection<Tier>(this._smartMobileContext.Tier.ToList());
                this.OperatorCollection = new ObservableCollection<PhoneVendor>(this._smartMobileContext.PhoneVendor.ToList());
            }
        }

        #region Commands

        public ICommandRoute SaveCommand { get; private set; }        
        public ICommandRoute ThoatCommand { get; private set; }
        public ICommandRoute DeleteCommand { get; private set; }
        public ICommandRoute DuyetCommand { get; private set; }

        #endregion

        #region Properties

        public ARCUST Arcus
        {
            get
            {
                return this._arcusModel;
            }

            set
            {
                this._arcusModel = value;
                base.RaisePropertyChanged("Arcus");
            }
        }

        public string CustomerName
        {
            get { return _customerName; }
            set
            {
                _customerName = value;
                base.RaisePropertyChanged("CustomerName");
            }
        }

        public string ShopName
        {
            get { return _shopName; }
            set
            {
                _shopName = value;
                base.RaisePropertyChanged("ShopName");
            }
        }

        public string Phone
        {
            get { return _phone; }
            set { _phone = value; base.RaisePropertyChanged("Phone"); }
        }

        public string Address
        {
            get { return _address; }
            set { _address = value; base.RaisePropertyChanged("Address"); }
        }

        public string Street
        {
            get { return _street; }
            set { _street = value; base.RaisePropertyChanged("Street"); }
        }

        public string Ward
        {
            get { return _ward; }
            set { _ward = value; base.RaisePropertyChanged("Ward"); }
        }

        public ObservableCollection<PROVINCE> ProvinceCollection
        {
            get { return this._provinceCollection; }
            set
            {
                this._provinceCollection = value;
                base.RaisePropertyChanged("ProvinceCollection");
            }
        }

        public ObservableCollection<City> CityCollection
        {
            get { return this._cityCollection; }
            set
            {
                this._cityCollection = value;
                base.RaisePropertyChanged("CityCollection");
            }
        }

        public ObservableCollection<Area> AreaCollection
        {
            get { return this._areaCollection; }
            set
            {
                this._areaCollection = value;
                base.RaisePropertyChanged("AreaCollection");
            }
        }

        public ObservableCollection<Region> RegionCollection
        {
            get { return this._regionCollection; }
            set
            {
                this._regionCollection = value;
                base.RaisePropertyChanged("RegionCollection");
            }
        }

        public ObservableCollection<GFKArea> GFKCollection
        {
            get { return this._gfkCollection; }
            set
            {
                this._gfkCollection = value;
                base.RaisePropertyChanged("GFKCollection");
            }
        }

        public ObservableCollection<Tier> TierCollection
        {
            get { return this._tierCollection; }
            set{
                this._tierCollection = value;
                base.RaisePropertyChanged("TierCollection");
            }
        }

        public ObservableCollection<PhoneVendor> OperatorCollection
        {
            get { return this._operatorCollection; }
            set
            {
                this._operatorCollection = value;
                base.RaisePropertyChanged("OperatorCollection");
            }
        }

        public ObservableCollection<TOWN> TownCollection
        {
            get { return this._townCollection; }
            set
            {
                this._townCollection = value;
                base.RaisePropertyChanged("TownCollection");
            }
        }

        public ObservableCollection<StrategicTypeModel> StrategicCollection
        {
            get { return this._strategicCollection; }
            set
            {
                this._strategicCollection = value;
                base.RaisePropertyChanged("StrategicCollection");
            }
        }

        public ObservableCollection<AROutletStatusModel> StatusCollection
        {
            get { return this._statusCollection; }
            set
            {
                this._statusCollection = value;
                base.RaisePropertyChanged("StatusCollection");
            }
        }

        public ObservableCollection<ARVolumeLevel> VolumeCollection
        {
            get { return this._volumeCollection; }
            set
            {
                this._volumeCollection = value;
                base.RaisePropertyChanged("VolumeCollection");
            }
        }

        public PROVINCE SelectedProvince
        {
            get { return this._selectedProvince; }
            set
            {
                this._selectedProvince = value;

                if (this._selectedProvince != null)
                {
                    var town = this._smartMobileContext.TOWN.Where(t => t.ProvinceKey == this._selectedProvince.ProvinceKey).ToList();
                    this.TownCollection = new ObservableCollection<TOWN>(town);
                }

                base.RaisePropertyChanged("SelectedProvince");
            }
        }

        public TOWN SelectedTown
        {
            get { return this._selectedTown; }
            set
            {
                this._selectedTown = value;
                base.RaisePropertyChanged("SelectedTown");
            }
        }

        public StrategicTypeModel SelectedStrategic
        {
            get { return this._selectedStrategic; }
            set
            {
                this._selectedStrategic = value;
                base.RaisePropertyChanged("SelectedStrategic");
            }
        }

        public ARVolumeLevel SelectedVolume
        {
            get { return this._selectedVolume; }
            set
            {
                this._selectedVolume = value;
                base.RaisePropertyChanged("SelectedVolume");
            }
        }

        public AROutletStatusModel SelectedStatus
        {
            get { return this._selectedStatus; }
            set
            {
                this._selectedStatus = value;
                base.RaisePropertyChanged("SelectedStatus");
            }
        }

        public City SelectedCity
        {
            get { return this._selectedCity; }
            set
            {
                this._selectedCity = value;
                base.RaisePropertyChanged("SelectedCity");
            }
        }

        public Area SelectedArea
        {
            get { return this._selectedArea; }
            set
            {
                this._selectedArea = value;
                base.RaisePropertyChanged("SelectedArea");
            }
        }

        public Region SelectedRegion
        {
            get { return this._selectedRegion; }
            set
            {
                this._selectedRegion = value;
                base.RaisePropertyChanged("SelectedRegion");
            }
        }

        public GFKArea SelectedGFK
        {
            get { return this._selectedGFK; }
            set
            {
                this._selectedGFK = value;
                base.RaisePropertyChanged("SelectedGFK");
            }
        }

        public PhoneVendor SelectedOperator
        {
            get { return this._selectedOperator; }
            set
            {
                this._selectedOperator = value;
                base.RaisePropertyChanged("SelectedOperator");
            }
        }

        public Tier SelectedTier
        {
            get { return this._selectedTier; }
            set
            {
                this._selectedTier = value;
                base.RaisePropertyChanged("SelectedTier");
            }
        }

        public bool IsGPS
        {
            get { return _isGPS; }
            set { _isGPS = value; }
        }

        public bool IsEdit
        {
            get { return _isEdit; }
            set { _isEdit = value; }
        }

        public bool IsDuyet { get; set; }

        public int NoOfSalesStaff
        {
            get
            {
                return this._noOfSalesStaff;
            }

            set
            {
                this._noOfSalesStaff = value;
                base.RaisePropertyChanged("NoOfSalesStaff");
            }
        }

        public string Email
        {
            get
            {
                return this._email;
            }

            set
            {
                this._email = value;
                base.RaisePropertyChanged("Email");
            }
        }

        public string Fax
        {
            get
            {
                return this._fax;
            }

            set
            {
                this._fax = value;
                base.RaisePropertyChanged("Fax");
            }
        }

        public double ShopWidth
        {
            get
            {
                return this._shopWidth;
            }

            set
            {
                this._shopWidth = value;
                base.RaisePropertyChanged("ShopWidth");
            }
        }

        public double ShopLong
        {
            get
            {
                return this._shopLong;
            }

            set
            {
                this._shopLong = value;
                base.RaisePropertyChanged("ShopLong");
            }
        }

        public string RetailChain
        {
            get
            {
                return this._retailChain;
            }

            set
            {
                this._retailChain = value;
                base.RaisePropertyChanged("RetailChain");
            }
        }

        public bool IsHeadquarterofchain
        {
            get
            {
                return this._isHeadquarterofchain; 
            }

            set
            {
                this._isHeadquarterofchain = value;
                base.RaisePropertyChanged("IsHeadquarterofchain");
            }
        }
        #endregion

        #region Overridens

        public override void Initialize(Uri uri)
        {
            //if (IsNetworkAvailable)
            //{
            //    this._proxy.GetProvinceCompleted += this.Proxy_GetProvinceCompleted;
            //    this._proxy.GetTownCompleted += this.Proxy_GetTownCompleted;
            //    this._proxy.GetProvinceAsync(GlobalInformation.SiteCode);
            //    this._proxy.GetTownAsync(GlobalInformation.SiteCode);
            //}
        }

        public override void PageUnloaded(Uri uri)
        {
            //this._proxy.GetTownCompleted -= this.Proxy_GetTownCompleted;
            //this._proxy.GetProvinceCompleted -= this.Proxy_GetProvinceCompleted;
        }

        #endregion

        #region Command Action Handlers

        private void ExecuteSaveCommand(object commandParams)
        {
            this.IsBusy = true;

            if (!this.Validate())
            {
                this.IsBusy = false;
                this.SaveCommand.RaiseCanExecuteChanged(true);

                MessageBox.Show("Vui lòng nhập đầy đủ thông tin.");
                return;
            }

            if (GlobalInformation.SelectedTCARCUS == null)
            {
                var maxID = this._smartMobileContext.TCARCUST.OrderByDescending(x => int.Parse(x.Customerkey)).First().ToString();
                if (maxID == null)
                {
                    maxID = "0";
                }
                else
                {
                    maxID = maxID.Replace("_", string.Empty);
                }

                int convertID = int.Parse(maxID) + 1;
                TCARCUST newArcust = new TCARCUST()
                {
                    ID_PK = Guid.NewGuid(),
                    Customerkey = "_" + convertID.ToString(),
                    Customername = this.CustomerName,
                    ShopOwner = this.ShopName,
                    Phone = this.Phone,
                    Address = this.Address,
                    Street = this.Street,
                    Ward = this.Ward,
                    Provice = this.SelectedProvince.ProvinceKey,
                    Town = this.SelectedTown.Townkey
                };

                // KH chỉnh sua lưu TCARCUST và set edit = 2 như nhân viên
                newArcust.IsEdit = "2";
                GlobalInformation.SelectedTCARCUS = newArcust;

                switch ((int)DateTime.Now.DayOfWeek)
                {
                    // Monday
                    case 1:
                        {
                            newArcust.T2 = "1";
                            break;
                        }
                    case 2:
                        newArcust.T3 = "1";
                        break;
                    case 3:
                        newArcust.T4 = "1";
                        break;
                    case 4:
                        newArcust.T5 = "1";
                        break;
                    case 5:
                        newArcust.T6 = "1";
                        break;
                    case 6:
                        newArcust.T7 = "1";
                        break;
                }

                ////if (this.IsDuyet)
                ////{
                ////    newArcust.Active = this._isApprove ? "1" : "0";
                ////}

                this._smartMobileContext.TCARCUST.InsertOnSubmit(newArcust);
                this._smartMobileContext.SubmitChanges();
            }
            else
            {
                // Delete old customer
                var currentCUs = (from ac in _smartMobileContext.TCARCUST where ac.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey select ac).FirstOrDefault();

                if (currentCUs != null)
                {
                    currentCUs.Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey;
                    GlobalInformation.SelectedTCARCUS.Customername = currentCUs.Customername = this.CustomerName;
                    GlobalInformation.SelectedTCARCUS.ShopOwner = currentCUs.ShopOwner = this.ShopName;
                    GlobalInformation.SelectedTCARCUS.Phone = currentCUs.Phone = this.Phone;
                    GlobalInformation.SelectedTCARCUS.Address = currentCUs.Address = this.Address;
                    GlobalInformation.SelectedTCARCUS.Street = currentCUs.Street = this.Street;
                    GlobalInformation.SelectedTCARCUS.Ward = currentCUs.Ward = this.Ward;
                    GlobalInformation.SelectedTCARCUS.Provice = currentCUs.Provice = this.SelectedProvince.ProvinceKey;
                    GlobalInformation.SelectedTCARCUS.Town = currentCUs.Town = this.SelectedTown.Townkey;
                    GlobalInformation.SelectedTCARCUS.IsEdit = currentCUs.IsEdit = "2";
                    ////if (this.IsDuyet)
                    ////{
                    ////    GlobalInformation.SelectedTCARCUS.Active = currentCUs.Active = this._isApprove ? "1" : "0";
                    ////}

                    this._smartMobileContext.SubmitChanges();
                }
            }

            //this.ShowMessage("Lưu thành công");

            if (this.IsGPS)
            {
                GlobalInformation.StateImageType = 0;
                base.PageNavigation.Navigate("ChupHinhMain");

            }
            else
            {
                this.ShowMessage("Lưu thành công");
                base.PageNavigation.Navigate("Main");
            }

            this.IsBusy = false;
            this.SaveCommand.RaiseCanExecuteChanged(true);
        }

        private void ExecuteThoatCommand(object commandParams)
        {
            GlobalInformation.StateImageType = 0;
            this.ThoatCommand.RaiseCanExecuteChanged(true);
            ////if (this.IsGPS)
            ////{
            ////    base.PageNavigation.Navigate("ChupHinhMain");
            ////}
            ////else if (this.IsDuyet)
            ////{
            ////    base.PageNavigation.Navigate(new Uri("DuyetTTLamTuyen?msg=" + "IsDuyet", UriKind.Relative));
            ////}
            ////else
            ////{
            ////    base.PageNavigation.Navigate("Main");
            ////}
        }

        private void ExecuteDeleteCommand(object obj)
        {
            this.DeleteCommand.RaiseCanExecuteChanged(true);

            // Delete customer
            this._smartMobileContext.TCARCUST.DeleteOnSubmit(this._smartMobileContext.TCARCUST.Where(t => t.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey).FirstOrDefault());
            this._smartMobileContext.SubmitChanges();

            if (GlobalInformation.ArSaleSPType.Usertype == "SS")
            {
                var currentApprovalcustomerinfor = _smartMobileContext.APPROVALCUSTOMERINFOR.FirstOrDefault(x => x.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey && x.Type == "INS");
                if (currentApprovalcustomerinfor != null)
                {
                    this._smartMobileContext.APPROVALCUSTOMERINFOR.DeleteOnSubmit(currentApprovalcustomerinfor);
                    this._smartMobileContext.SubmitChanges();
                }

                APPROVALCUSTOMERINFOR approvalcustomerinfor = new APPROVALCUSTOMERINFOR();
                approvalcustomerinfor.ID_PK = Guid.NewGuid();
                approvalcustomerinfor.Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey;
                approvalcustomerinfor.Key = "3";
                approvalcustomerinfor.Type = "INS";

                _smartMobileContext.APPROVALCUSTOMERINFOR.InsertOnSubmit(approvalcustomerinfor);
            }
            else
            {
                //
            }            

            _smartMobileContext.SubmitChanges();
            MessageBox.Show("Hoàn thành bỏ khỏi Tuyến cho Khách Hàng này");
            base.PageNavigation.Navigate("Main");
        }

        private void ExecuteDuyetCommand(object obj)
        {
            this.DuyetCommand.RaiseCanExecuteChanged(true);
           
            ////this._isApprove = true;
            var currentApprovalcustomerinfor = _smartMobileContext.APPROVALCUSTOMERINFOR.FirstOrDefault(x => x.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey && x.Type == "INS");
            if (currentApprovalcustomerinfor == null)
            {
                APPROVALCUSTOMERINFOR approvalcustomerinfor = new APPROVALCUSTOMERINFOR();
                approvalcustomerinfor.ID_PK = Guid.NewGuid();
                approvalcustomerinfor.Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey;
                approvalcustomerinfor.Key = "1";
                approvalcustomerinfor.Type = "INS";
                _smartMobileContext.APPROVALCUSTOMERINFOR.InsertOnSubmit(approvalcustomerinfor);
            }
            else
            {
                currentApprovalcustomerinfor.Key = "1";
                currentApprovalcustomerinfor.Type = "INS";
            }

            _smartMobileContext.SubmitChanges();
            MessageBox.Show("Hoàn thành duyệt Khách Hàng");
        }

        #endregion

        #region Web-service Handlers

        //private void Proxy_GetProvinceCompleted(object sender, GetProvinceCompletedEventArgs e)
        //{
        //    ArrayOfXElement result = e.Result;

        //    List<XmlProvince> provinceList = XmlHelper.GetItems<XmlProvince>(e.Result.Nodes[1], "Table").ToList();

        //    List<SqlProvince> provinces = new List<SqlProvince>();
        //    SqlProvince localProvince = null;

        //    this.ProvinceCollection = new ObservableCollection<ProvinceModel>();

        //    // converts to savable object-list
        //    foreach (XmlProvince province in provinceList)
        //    {
        //        localProvince = new SqlProvince
        //        {
        //            ProvinceCode = province.ProvinceCode,
        //            ProvinceName = province.ProvinceName
        //        };

        //        provinces.Add(localProvince);

        //        this.ProvinceCollection.Add(new ProvinceModel(localProvince));
        //    }

        //    this.ProvinceCollection.Insert(0, EmptyProvince);
        //    this.SelectedProvince = this.ProvinceCollection.First();

        //    // writes to local storage
        //    DataSourceService.Current.Write<List<SqlProvince>>("Province", "ProvinceList", provinces);
        //}

        //private void Proxy_GetTownCompleted(object sender, GetTownCompletedEventArgs e)
        //{
        //    ArrayOfXElement result = e.Result;

        //    List<XmlTown> townList = XmlHelper.GetItems<XmlTown>(e.Result.Nodes[1], "Table").ToList();

        //    List<SqlTown> towns = new List<SqlTown>();
        //    SqlTown localTown = null;

        //    // converts to savable object-list
        //    foreach (XmlTown tw in townList)
        //    {
        //        localTown = new SqlTown
        //        {
        //            AppStatus = tw.AppStatus,
        //            ProvinceCode = tw.ProvinceCode,
        //            TownCode = tw.TownCode,
        //            TownName = tw.TownName
        //        };

        //        towns.Add(localTown);

        //        this._townList.Add(new TownModel(localTown));
        //    }

        //    // writes to local storage
        //    DataSourceService.Current.Write<List<SqlTown>>("Town", "TownList", towns);
        //}

        //private void Proxy_InsertCustomerkeyCompleted(object sender, InsertCustomerkeyCompletedEventArgs e)
        //{
        //    throw new NotImplementedException();
        //}

        #endregion

        #region Method

        private void CreateConnection()
        {
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;
        }

        private bool Validate()
        {
            if (string.IsNullOrEmpty(this.Address) || string.IsNullOrEmpty(this.CustomerName) ||
                string.IsNullOrEmpty(this.Phone) ||
                this.SelectedProvince == null || string.IsNullOrEmpty(this.Street) ||
                this.SelectedTown == null || string.IsNullOrEmpty(this.Ward))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        #endregion

    }
}
